#include <fstream>
#include <string>
#include "Light.h"

using namespace std;

static mUInt uLightNum = 0;

string LightTypes[3] = 
{
    "Point",
    "Spot",
    "Directional"
};

CLight::CLight( mString a_sFileName )
{
    m_bEnabled = true;
    m_uLightNum = uLightNum;
    uLightNum++;
    ReadFromFile(a_sFileName);
}

mVoid CLight::Enable(mBool a_bValue)
{
    m_bEnabled = a_bValue;
}

CVector4f& CLight::Dir( )
{
    return m_L2W.Dir();
}

CVector4f& CLight::Pos( )
{
    return m_L2W.Pos();
}


mVoid CLight::SendToShader( CIShader *a_pIShader )
{
    a_pIShader->SendConstantValue("g_vLightA", &m_LightProp.vAmbient , 12, EveryFrame);
    a_pIShader->SendConstantValue("g_vLightD", &m_LightProp.vDiffuse , 12, EveryFrame);
    a_pIShader->SendConstantValue("g_vLightS", &m_LightProp.vSpecular, 12, EveryFrame);

    a_pIShader->SendConstantValue("g_fLightRange"  , &m_LightProp.fRange  , 4, EveryFrame);
    a_pIShader->SendConstantValue("g_fLightFallOff", &m_LightProp.fFalloff, 4, EveryFrame);

    a_pIShader->SendConstantValue("g_fLightTheta"  , &m_LightProp.fTheta  , 4, EveryFrame);
    a_pIShader->SendConstantValue("g_fLightPhi"    , &m_LightProp.fPhi    , 4, EveryFrame);

    a_pIShader->SendConstantValue("g_fLightAtt0"   , &m_LightProp.fAtt0   , 4, EveryFrame);
    a_pIShader->SendConstantValue("g_fLightAtt1"   , &m_LightProp.fAtt0   , 4, EveryFrame);
    a_pIShader->SendConstantValue("g_fLightAtt2"   , &m_LightProp.fAtt0   , 4, EveryFrame);    

    a_pIShader->SendConstantValue("g_vLightDir", &m_L2W[8] , 12, EveryFrame);
    a_pIShader->SendConstantValue("g_vLightPos", &m_L2W[12], 12, EveryFrame);
}

mVoid CLight::ReadFromFile( mString a_sFileName )
{
    ifstream Input(a_sFileName.c_str());
    string LightType;
    mUInt LightTypeInd;
    Input >> LightType;
    for (LightTypeInd = 0; LightTypeInd < 3; LightTypeInd++)
    {
        if (LightType.find(LightTypes[LightTypeInd]) != string::npos )
        {
            m_LightProp.Type = TLightType(LightTypeInd + 1);
            break;
        }
    }

    Input >> m_LightProp.vDiffuse[0];
    Input >> m_LightProp.vDiffuse[1];
    Input >> m_LightProp.vDiffuse[2];
    Input >> m_LightProp.vDiffuse[3];

    Input >> m_LightProp.vSpecular[0];
    Input >> m_LightProp.vSpecular[1];
    Input >> m_LightProp.vSpecular[2];
    Input >> m_LightProp.vSpecular[3];

    Input >> m_LightProp.vAmbient[0];
    Input >> m_LightProp.vAmbient[1];
    Input >> m_LightProp.vAmbient[2];
    Input >> m_LightProp.vAmbient[3];

    Input >> m_LightProp.fRange;
    Input >> m_LightProp.fFalloff;

    Input >> m_LightProp.fAtt0;
    Input >> m_LightProp.fAtt1;
    Input >> m_LightProp.fAtt2;

    Input >> m_LightProp.fTheta;
    Input >> m_LightProp.fPhi;

    m_LightProp.fTheta *= M_PI / 180.0f;
    m_LightProp.fPhi   *= M_PI / 180.0f;

}

